package psn.kiko.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应 2024/11/10 13:22
 */
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("统一响应结构")
public class R<D>{
	
	@ApiModelProperty("响应信息")
	private String msg;
	@ApiModelProperty("响应数据")
	private D data;
	@ApiModelProperty("响应业务码:0-异常业务，1-正常业务")
	private int code;
	private static final int OK = 1;
	private static final int FAIL = 0;
	
	//成功
	public static R<?> ok(){
		return R.builder().code(OK).msg("请求成功").build();
	}
	
	public static R<?> okWithData(Object data){
		return R.builder().code(OK).msg("请求成功").data(data).build();
	}
	
	public static R<?> okWithMsg(String msg){
		return R.builder().code(OK).msg(msg).build();
	}
	
	public static R<?> okWithMsgAndData(String msg,Object data){
		return R.builder().code(OK).msg(msg).data(data).build();
	}
	
	//失败
	public static R<?> fail(){
		return R.builder().code(FAIL).msg("请求失败").build();
	}
	
	public static R<?> failWithMsg(String msg){
		return R.builder().code(FAIL).msg(msg).build();
	}
	
	public static R<?> failWithData(Object data){
		return R.builder().code(FAIL).data(data).build();
	}
	
	public static R<?> failWithMsgAndData(String msg,Object data){
		return R.builder().code(FAIL).msg(msg).data(data).build();
	}
}
